package com.lkl.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.lkl.entity.Address;
import lombok.SneakyThrows;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 数据库操作工具类
 * @param <T>
 */
public  abstract class DruidUtil<T> {
    private  Connection conn ;
    private  PreparedStatement pst;
    private static DruidDataSource ds = new DruidDataSource();

    static {
        System.out.println("加载配置文件");
        Properties pro = new Properties();
        InputStream is = DruidUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ds.configFromPropety(pro);

    }

    @SneakyThrows
    public static Connection getConn()  {
        return  ds.getConnection();
    }

    public static void close(PreparedStatement pst){
        try {
            if (pst != null) {
                pst.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet rs, PreparedStatement pst){
        try {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     *查询对象数据
     *
     * @param sql
     * @param obj
     * @return
     */
    @SneakyThrows
    public List<T> query(String sql, Object... obj){
        ArrayList<T> list = new ArrayList<>();
        conn = getConn();
        pst = conn.prepareStatement(sql);
        for (int i = 0; i < obj.length; i++) {

            pst.setObject(i+1, obj[i]);
        }
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            T entity = getResultSet(rs);
            list.add(entity);
        }
        close(rs,pst);
        return list;
    }

    /**
     *查询对象数据条数
     *
     * @param sql
     * @param obj
     * @return
     */
    @SneakyThrows
    public Long countData(String sql, Object... obj){
        long count = 0L;
        conn = getConn();
        pst = conn.prepareStatement(sql);
        for (int i = 0; i < obj.length; i++) {
            pst.setObject(i+1, obj[i]);
        }
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            count = rs.getLong(1);

        }
        close(rs,pst);
        return count;
    }
    @SneakyThrows
    public boolean upData(String sql, Object... obj){
        conn = getConn();
        pst = conn.prepareStatement(sql);
        for (int i = 0; i < obj.length; i++) {

            pst.setObject(i+1,obj[i]);
        }
        int flag = pst.executeUpdate();
        close(pst);
        if (flag > 0){
            return true;
        }
        return false;
    }
    public void insertBatch(String sql, Object...obj)  {
        long start = System.currentTimeMillis();
        conn = getConn();
        try {
            pst = conn.prepareStatement(sql);
            for (Object o : obj) {
                Address addresses = (Address) o;
                pst.setObject(1, addresses.getCode());
                pst.setObject(2, addresses.getName());
                pst.setObject(3, addresses.getParentCode());
                pst.addBatch();
            }
            pst.executeBatch();
            pst.clearBatch();
            conn.commit();//所有语句都执行完毕后才手动提交sql语句
            System.out.println("提交数据成功！");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(pst);
        }
        System.out.println(System.currentTimeMillis() - start);
    }
    public abstract T getResultSet(ResultSet rs) throws SQLException;
}
